package com.tinem.platform.service.channel.api;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONObject;
import com.tinem.platform.module.commons.util.PasswordUtil;
import com.tinem.platform.module.pojo.enums.security.EncryptionAlgorithm;
import com.tinem.platform.module.pojo.enums.security.SignatureAlgorithm;
import com.tinem.platform.module.pojo.vo.channel.*;
import com.tinem.platform.module.pojo.vo.config.ConfigWebAuthIpWhiteInfo;
import com.tinem.platform.module.pojo.vo.config.ConfigWebFileClientInfo;
import com.tinem.platform.module.starter.jpa.entity.client.ClientChannelDetails;
import com.tinem.platform.module.starter.jpa.entity.client.ClientChannelSecurity;
import com.tinem.platform.module.starter.jpa.entity.config.ConfigWebAuthIpWhite;
import com.tinem.platform.module.starter.jpa.entity.config.ConfigWebFileClient;
import com.tinem.platform.module.starter.jpa.repository.client.ClientChannelDetailsRepository;
import com.tinem.platform.module.starter.jpa.repository.client.ClientChannelSecurityRepository;
import com.tinem.platform.module.starter.jpa.repository.config.ConfigWebFileClientRepository;
import com.tinem.platform.module.starter.jpa.repository.rbac.ConfigWebAuthIpWhiteRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigInteger;

/**
 * @author fzh
 * @version v1
 * @program: platform
 * @className ChannelApi
 * @description
 * @site
 * @company
 * @create 2021-11-03 11:49
 */

@Slf4j
@Api(value = "客户端渠道")
@RestController
@RequestMapping("/")
public class ChannelApi {

    @Resource
    ClientChannelDetailsRepository clientChannelDetailsRepository;
    @Resource
    ClientChannelSecurityRepository clientChannelSecurityRepository;
    @Resource
    ConfigWebAuthIpWhiteRepository configWebAuthIpWhiteRepository;
    @Resource
    ConfigWebFileClientRepository configWebFileClientRepository;

    @PostMapping("/client.api")
    @ApiOperation(value = "客户端渠道信息查询", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name="clientId",value="客户端渠道编号",required=true)
    })
    public ClientChannelDetails client(@RequestBody JSONObject req){
        log.info("client:{}",req.getString("clientId"));
        return clientChannelDetailsRepository.findById(req.getString("clientId")).get();
    }

    @PostMapping("/clientSecurity.api")
    @ApiOperation(value = "客户端渠道安全信息查询", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name="clientId",value="客户端渠道编号",required=true)
    })
    public ClientChannelSecurity clientSecurity(@RequestBody JSONObject req){
        log.info("client:{}",req.getString("clientId"));
        return clientChannelSecurityRepository.findById(req.getString("clientId")).get();
    }

    @PostMapping("/addOne.api")
    @ApiOperation(value = "添加1.0渠道", httpMethod = "POST")
    public Object addOne(@RequestBody AddOne addOne){
        AddReq req = new AddReq();

        ClientChannelDetailsInfo clientChannelDetailsInfo = new ClientChannelDetailsInfo();
        clientChannelDetailsInfo.setClietId("one_"+addOne.getDomain());
        clientChannelDetailsInfo.setClientName("one_"+addOne.getDomain());
        clientChannelDetailsInfo.setResourceIds("all");
        clientChannelDetailsInfo.setClientSecret(addOne.getSecret());
        clientChannelDetailsInfo.setScope("platform-cloud-business-one-"+addOne.getDomain()+",platform-cloud-service-data,platform-cloud-service-link");
        clientChannelDetailsInfo.setAuthorizedGrantTypes("client_credentials,client_credentials_custom");
        clientChannelDetailsInfo.setWebServerRedirectUri("");
        clientChannelDetailsInfo.setAuthorities("");
        clientChannelDetailsInfo.setAccessTokenValidity(604800);
        clientChannelDetailsInfo.setRefreshTokenValidity(604800);
        clientChannelDetailsInfo.setAdditionalInformation("");
        clientChannelDetailsInfo.setAutoapprove("true");
        req.setClientChannelDetailsInfo(clientChannelDetailsInfo);

        ClientChannelSecurityInfo clientChannelSecurityInfo = new ClientChannelSecurityInfo();
        clientChannelSecurityInfo.setReqSign(false);
        clientChannelSecurityInfo.setReqSignType(SignatureAlgorithm.SHA256_WITH_RSA);
        clientChannelSecurityInfo.setReqSignKey("");
        clientChannelSecurityInfo.setResSign(false);
        clientChannelSecurityInfo.setResSignType(SignatureAlgorithm.SHA256_WITH_RSA);
        clientChannelSecurityInfo.setResSignKey("");
        clientChannelSecurityInfo.setReqCrypto(false);
        clientChannelSecurityInfo.setReqCryptoType(EncryptionAlgorithm.AES_ECB_PKCS5PADDING);
        clientChannelSecurityInfo.setReqCryptoIv("");
        clientChannelSecurityInfo.setReqCryptoKey("");
        clientChannelSecurityInfo.setReqCryptoPuk("");
        clientChannelSecurityInfo.setResCrypto(false);
        clientChannelSecurityInfo.setResCryptoType(EncryptionAlgorithm.AES_ECB_PKCS5PADDING);
        clientChannelSecurityInfo.setResCryptoIv("");
        clientChannelSecurityInfo.setResCryptoKey("");
        clientChannelSecurityInfo.setResCryptoPpk("");
        req.setClientChannelSecurityInfo(clientChannelSecurityInfo);

        ConfigWebAuthIpWhiteInfo configWebAuthIpWhiteInfo = new ConfigWebAuthIpWhiteInfo();
        configWebAuthIpWhiteInfo.setIpWhite("172.20.0.0/16");
        req.setConfigWebAuthIpWhiteInfo(configWebAuthIpWhiteInfo);

        ConfigWebFileClientInfo configWebFileClientInfo = new ConfigWebFileClientInfo();
        configWebFileClientInfo.setChennelFilePrefix("/mnt/one/"+addOne.getDomain()+"/");
        configWebFileClientInfo.setChennelUrlPrefix("https://www."+addOne.getDomain()+".com/upload/{fileName}");
        configWebFileClientInfo.setChennelFileMaxSize(BigInteger.valueOf(104857600));
        configWebFileClientInfo.setChennelStorageSize(BigInteger.valueOf(107374182400L));
        configWebFileClientInfo.setChennelUseStorageSize(BigInteger.ZERO);
        req.setConfigWebFileClientInfo(configWebFileClientInfo);

        return  add(req);
    }

    @PostMapping("/add.api")
    @ApiOperation(value = "添加渠道", httpMethod = "POST")
    public Object add(@RequestBody AddReq req){
        ClientChannelDetails clientChannelDetails = new ClientChannelDetails();
        BeanUtil.copyProperties(req.getClientChannelDetailsInfo(),clientChannelDetails);

        ClientChannelSecurity clientChannelSecurity = new ClientChannelSecurity();
        BeanUtil.copyProperties(req.getClientChannelSecurityInfo(),clientChannelSecurity);

        ConfigWebAuthIpWhite configWebAuthIpWhite = new ConfigWebAuthIpWhite();
        BeanUtil.copyProperties(req.getConfigWebAuthIpWhiteInfo(),configWebAuthIpWhite);

        ConfigWebFileClient configWebFileClient = new ConfigWebFileClient();
        BeanUtil.copyProperties(req.getConfigWebFileClientInfo(),configWebFileClient);

        clientChannelDetails.setClientSecret(PasswordUtil.encode(clientChannelDetails.getClientSecret()));
        clientChannelSecurity.setId(clientChannelDetails.getClietId());
        configWebAuthIpWhite.setId(clientChannelDetails.getClietId());
        configWebFileClient.setId(clientChannelDetails.getClietId());

        clientChannelDetailsRepository.save(clientChannelDetails);
        clientChannelSecurityRepository.save(clientChannelSecurity);
        configWebAuthIpWhiteRepository.save(configWebAuthIpWhite);
        configWebFileClientRepository.save(configWebFileClient);

        AddRes res = new AddRes();
        res.setClientChannelDetailsInfo(new ClientChannelDetailsInfo());
        BeanUtil.copyProperties(clientChannelDetailsRepository.findById(clientChannelDetails.getClietId()).get(),res.getClientChannelDetailsInfo());

        res.setClientChannelSecurityInfo(new ClientChannelSecurityInfo());
        BeanUtil.copyProperties(clientChannelSecurityRepository.findById(clientChannelDetails.getClietId()).get(),res.getClientChannelSecurityInfo());

        res.setConfigWebAuthIpWhiteInfo(new ConfigWebAuthIpWhiteInfo());
        BeanUtil.copyProperties(configWebAuthIpWhiteRepository.findById(clientChannelDetails.getClietId()).get(),res.getConfigWebAuthIpWhiteInfo());

        res.setConfigWebFileClientInfo(new ConfigWebFileClientInfo());
        BeanUtil.copyProperties(configWebFileClientRepository.findById(clientChannelDetails.getClietId()).get(),res.getConfigWebFileClientInfo());

        return res;
    }
}
